﻿using BHan.Context;
using BHan.Model.Entity.Pojo;
using BHan.Model.Enums;
using BHan.Repository.IRepository;
using BHan.Util;
using BHan.View.VMenu;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
 
using HandyControl.Data;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;

namespace BHan.VM.MenuVM
{
    public partial class PageMenuVM : ObservableObject,INotifyPropertyChanged
    {
        protected readonly IMenuRepository _menuRepository;

        [ObservableProperty]
        public List<MenuModel> menuList = new List<MenuModel>();

        [ObservableProperty]
        public List<string> stateList = new List<string>();

        [ObservableProperty]
        public MenuModel menuModel = new MenuModel();

        [ObservableProperty]
        public MenuModel pMenuIdModel = new MenuModel();

        [ObservableProperty]
        public ObservableCollection<MenuModel> datas = new ObservableCollection<MenuModel>();

        [ObservableProperty]
        public int maxCount = 20;

        [ObservableProperty]
        public int pageIndex = 1;

        [ObservableProperty]
        public int dataCountPerPage = 20;

        [ObservableProperty]
        private string searchText;


        public PageMenuVM()
        {
            _menuRepository = App.Current.Services.GetService<IMenuRepository>();

            SearchInit();
        }

        private void SearchInit()
        {
            string search = SearchText;
            var list = _menuRepository.GetMenuList(search);
            MenuList = list;
            var da = list.Skip(0).Take(dataCountPerPage).ToList();
            da.ForEach(m =>
            {
                datas.Add(m);
            });
            stateList = EnumConverterUtil.EnumToList<StateEnum>();
        }

        /// <summary>
        ///  分页切换事件
        /// </summary>
        /// <param name="parameter"></param>
        [RelayCommand]
        private void PageUpdated(object parameter)
        {
            if (parameter is FunctionEventArgs<int> args)
            {
                var list = _menuRepository.GetMenuList(SearchText);
                var da = list.Skip((args.Info - 1) * maxCount).Take(maxCount).ToList();
                datas.Clear();
                da.ForEach(m =>
                {
                    datas.Add(m);
                });
            }
        }

        /// <summary>
        ///  新增按钮
        /// </summary>
        [RelayCommand]
        private void AddMenu() {

            WindowMenuEdit edit = (WindowMenuEdit)App.Current.Services.GetServiceByName<Window>("WindowMenuEdit");
            edit.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            var res = edit.ShowDialog();
            if (res.Value)
            {
                MenuModel.delFlag = false;
                _menuRepository.Create(MenuModel);
            }
             
        }

        [RelayCommand]
        private void AddMenuById(int id)
        {
            WindowMenuEdit edit = (WindowMenuEdit)App.Current.Services.GetServiceByName<Window>("WindowMenuEdit");
            edit.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            var res = edit.ShowDialog();
            if (res.Value)
            {
                MenuModel.pMenuId = id;
                MenuModel.delFlag = false;
                _menuRepository.Create(MenuModel);
            }
             
        }

        /// <summary>
        /// 修改编辑
        /// </summary>
        /// <param name="id"></param>
        [RelayCommand]
        private void UpdateMenu(int id)
        {
            menuModel = _menuRepository.GetMenuModelById(id);
            WindowMenuEdit edit = (WindowMenuEdit)App.Current.Services.GetServiceByName<Window>("WindowMenuEdit");
            edit.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            var res = edit.ShowDialog();
            if (res.Value)
            {
                MenuModel.delFlag = false;
                _menuRepository.Update(MenuModel);
            }        
        }

        /// <summary>
        /// 搜索按钮
        /// </summary>
        [RelayCommand]
        private void SearchMenu()
        {
            datas.Clear();
            string search = SearchText;
            var list = _menuRepository.GetMenuList(search);
            MenuList = list;
            var da = list.Skip(0).Take(DataCountPerPage).ToList();
            da.ForEach(m =>
            {
                datas.Add(m);
            });
            stateList = EnumConverterUtil.EnumToList<StateEnum>();
             
        }

        /// <summary>
        /// 根据 id 删除菜单
        /// </summary>
        /// <param name="id"></param>
        [RelayCommand]
        private void DeleteMenu(int id)
        {
            MenuModel menu = _menuRepository.Get(id);
            menu.delFlag = true;
            int rows = _menuRepository.Update(menu);
            if(rows > 0)
            {
                HandyControl.Controls.MessageBox.Success("删除成功！");
                datas.Clear();
                var list = _menuRepository.GetMenuList(SearchText);
                MenuList = list;
                var da = list.Skip(0).Take(dataCountPerPage).ToList();
                da.ForEach(m =>
                {
                    datas.Add(m);
                });
            }
            else
            {
                HandyControl.Controls.MessageBox.Info("删除失败！");
            }
        }
          
    }
}
